<?php
/**
*注册控制器
*/
namespace Home\Controller;
use Think\Controller;
class RegisterController extends CommonController
{
    public function index()
    {	
    	 $sql = "select * from vercode order by rand() limit 1";
    	$res = M()->query($sql);
    	$_SESSION['answer']=$res[0]['answer'];
    	$this->assign('res',$res);
        $this->display();
    }

    public function checkemail()
    {
      	$email =I('post.email','');
       $preg = "/^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/";
       $c = preg_match($preg,$email);
	       if($c==0){
	            $arr = array(
	            	'error'=>0,
	            	'info' =>"邮箱格式错误！", 
	            	);
	            echo json_encode($arr);
	            exit;
	    		}
		$sql ="select * from user where email='{$email}'";
		$res =M()->query($sql);
			if ($res) {
				$arr = array(
					'error'=>0,
					'info'=>"该邮箱以存在，请重新填写！",	
					);
				echo json_encode($arr);
			}else{
				$arr = array(
					'error'=>1,
					'info'=>"该邮箱可以使用！",	
					);
				echo json_encode($arr);
			}

    }
    public function checkenikename()
    {
    	$nikename = $_POST["nikename"];
    	$preg='/^[a-zA-Z0-9\x{4e00}-\x{9fa5}\_&\*\.\^@%*?]{2,7}$/u';
    	$c= preg_match($preg, $nikename);
	    	if($c==0){
	            $arr = array(
	            	'error'=>0,
	            	'info' =>"该昵称不合法，长度为2-7位，请重新写", 
	            	);
	            echo json_encode($arr);
	            exit;
	    		}
		$sql ="select * from user where nikename='{$nikename}'";
		$res =M()->query($sql);
			if ($res) {
				$arr = array(
					'error'=>0,
					'info'=>"该昵称以存在，请重新填写！",	
					);
				echo json_encode($arr);
			}else{
				$arr = array(
					'error'=>1,
					'info'=>"该昵称可以使用！",	
					);
				echo json_encode($arr);
			}
    }

    public function checkecode()
    {
    	$vercode =I('post.vercode','');

    	
    	if($vercode!=$_SESSION['answer']){
    		$arr = array(
    			'error'=>1,
    			'info'=>'人类验证不通过'
    			);
    		echo json_encode($arr);
    	}else{
    		$arr = array(
    			'error'=>0,
    			'info'=>'通过'
    			);
    		echo json_encode($arr);
    	}
    }
    public function add()
    {
    	$data = I('post.');
    	$data['password'] = md5($data['password']);
    	$data['create_time'] = time();

    	$res = M('user')->add($data);

    	if($res){
    		$arr = array(
    			'error'=>0,
    			'info'=>'注册成功，请登录'
    			);
    		echo json_encode($arr);
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info'=>'注册失败，重新注册'
    			);
    		echo json_encode($arr);
    	}
    }

    public function getcode()
    {
    	$sql = "select * from vercode order by rand() limit 1";
    	$res = M()->query($sql);
    	$_SESSION['answer']=$res[0]['answer'];
    	echo json_encode(array("info"=>$res[0]['code']));
    }

}
